<template>
  <j-modal
    :title="title"
    :width="700"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules">
        <a-form-model-item label="配置名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入配置名称" v-model="ruleForm.configName"> </a-input>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, putAction, postAction } from '@/api/manage'

export default {
  name: 'ProcessConditionConfigsFormModal',
  data() {
    return {
      form: this.$form.createForm(this),
      processId: '',
      title: '流程条件配置',
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      },
      confirmLoading: false,
      url: {
        edit: '/property/act/processConditionConfigs'
      },
      record: {},
      isEdit: false,
      ruleForm: {
        configName: ''
      },
      rules: {
        configName: [{ required: true, message: '值不能为空', trigger: 'blur' }]
      }
    }
  },
  props: {
    actProcessId: null,
    deploymentId: null
  },
  created() {},
  methods: {
    close() {
      this.$emit('close')
      this.visible = false
    },
    add(record) {
      this.ruleForm.configName = ''
      this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
      this.visible = true
      this.isEdit = false
    },
    edit(record) {
      const that = this
      this.visible = true
      this.isEdit = true
      this.form.resetFields()
      getAction(that.url.edit + '/' + record.id).then(res => {
        if (res.success) {
          this.$nextTick(() => {
            this.ruleForm.configName = res.result.configName
          })
          this.record = res.result
        }
      })
    },
    handleOk() {
      const that = this
      this.$refs.ruleForm.validate((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
        } else {
          if (this.isEdit) {
            var params = {
              id: this.record.id,
              configName: this.ruleForm.configName
            }
            putAction(that.url.edit, params).then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.visible = false
              } else {
                that.$message.error(res.message)
              }
            })
          } else {
            var params = {
              configName: this.ruleForm.configName,
              actProcessId: this.actProcessId,
              deploymentId: this.deploymentId
            }
            postAction(that.url.edit, params).then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.visible = false
              } else {
                that.$message.error(res.message)
              }
            })
          }
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
